import { NextResponse } from "next/server";
import pageModule from "@/modules/page";
import {TIP_TAP_DEFAULT} from "@/config/settings";
import templates from "@/template/pages/config";

export async function GET(request, { params }) {
  const codeMap = { 0: "获取成功", 1: "获取失败", };
  const { id } = await params;
  const page = await pageModule.getById(id);
  if (!page) {
    return NextResponse.json({ code: 1, codeMap, message: "Page not found" }, { status: 404 });
  }
  const content = page.content ? JSON.parse(page.content) : JSON.stringify(TIP_TAP_DEFAULT);
  return NextResponse.json({code: 0, codeMap, message: "success", data: {...page, content,templates}});
}

export async function DELETE(request, { params }) {
  const codeMap = { 0: "删除成功", 1: "页面不存在", 2: "删除失败" };
  const { id } = await params;
  const page = await pageModule.getById(id);
  if (!page) { return NextResponse.json({ code: 1, codeMap, message: "Page not found" }, { status: 404 }); }
  const res = await pageModule.deleteById(id);
  if (!res) { return NextResponse.json({ code: 2, codeMap, message: "Delete failed" }, { status: 500 }); }
  return NextResponse.json({ code: 0, codeMap, message: "success" });
}